package com.ayoka.ui.meditation;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;

import com.ayoka.R;
import com.ayoka.base.BaseActivity;
import com.ayoka.databinding.ActivityMeditationDetailBinding;
import com.ayoka.mvp.MeditationMVP.MeditationPresenter;
import com.ayoka.mvp.MeditationMVP.IMeditationView;
import com.ayoka.mvp.MeditationMVP.MeditationModel.MeditationBean;
import com.ayoka.utils.AudioManagerEvent;
import com.ayoka.utils.MediaManager;
import com.jcp.baselibrary.model.ToolBarOptions;
import com.jcp.baselibrary.utils.DateUtil;
import com.jcp.baselibrary.utils.StatusBarUtils;
import com.jcp.baselibrary.utils.TDevice;
import com.jcp.baselibrary.utils.UIUtils;

import java.util.Observable;

/**
 * @author jcp
 * @ClassName: TopicListActivity
 * @Description:
 * @date 2018-12-28 下午4:21:18
 */
public class MeditationDetailActivity extends BaseActivity<ActivityMeditationDetailBinding, MeditationPresenter> implements IMeditationView {
    private static int OPERATE_ADD_RECORD = 0x001;
    private static int OPERATE_ADD_NOTE = 0x002;
    private static int OPERATE_COLLECT = 0x003;
    private static int OPERATE_CANCEL_COLLECT = 0x004;

    public static void start(Activity activity, int meditationId, int taskId) {
        Intent intent = new Intent(activity, MeditationDetailActivity.class);
        intent.putExtra("MeditationId", meditationId);
        intent.putExtra("TaskId", taskId);
        activity.startActivityForResult(intent, REQUEST_CODE_FOR_RESULT);
    }

    private int meditationId;
    private int taskId;
    private MeditationBean meditationBean;
    private int duration;

    private int operateType;
    private int recordId;

    @Override
    protected void configIntentValue() {
        meditationId = getIntent().getIntExtra("MeditationId", 0);
        taskId = getIntent().getIntExtra("TaskId", 0);
    }

    @Override
    protected void onBeforeSetContentLayout() {
        StatusBarUtils.setStatusBar(this, false, false);
        StatusBarUtils.setStatusTextColor(this, false);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_meditation_detail;
    }

    @Override
    protected void initViews() {
        mDataBinding.tvSelectVr.setOnClickListener(this);
        mDataBinding.btnPlay.setOnClickListener(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions("冥想");
        options.backgroundResource = 0;
        options.navigateId = R.mipmap.icon_back_white;
        options.titleColor = Color.WHITE;
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        // 设置监听
        MediaManager.getInstance().addObserver(this);
        AudioManagerEvent.getInstance().addObserver(this);
        AudioManagerEvent.getInstance().requestAudioFocus();
        mPresenter.getMeditationDetailData(meditationId, taskId);
    }

    @Override
    protected MeditationPresenter getPresenter() {
        return new MeditationPresenter();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == mDataBinding.btnPlay) {
            if (TDevice.isInstallByPackageName(this, "com.ayuk.Acrophobia")){
                TDevice.openAppActivity(this, "com.ayuk.Acrophobia", "com.unity3d.player.UnityPlayerActivity");
            } else {
                UIUtils.openBrowser(this, "https://www.pgyer.com/NnUz");
            }
        }
        if (view == mDataBinding.btnPlay) {
            onPlayClick(view);
        }
    }

    private void onPlayClick(View view) {
        if (MediaManager.getInstance().getPlayStatus() == MediaManager.PLAY) {
            MediaManager.getInstance().pause();
        } else if (MediaManager.getInstance().getPlayStatus() == MediaManager.PAUSE) {
            MediaManager.getInstance().resume();
        } else if (MediaManager.getInstance().getPlayStatus() == MediaManager.STOP) {
            MediaManager.getInstance().start();
        } else if (MediaManager.getInstance().getPlayStatus() == MediaManager.RELEASE) {
            MediaManager.getInstance().setMediaDataSource(this, "http://music.163.com/song/media/outer/url?id=281951.mp3");
            mDataBinding.btnPlay.setOnClickListener(null);
            mDataBinding.progressLoading.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void configMeditationDetailData(MeditationBean data) {
        this.meditationBean = data;
        mDataBinding.tvTitle.setText("冥想");
        mDataBinding.tvName.setText(meditationBean.CreateDate);
        invalidateOptionsMenu();

        mPresenter.showStartMeditationDialog(this);
    }

    @Override
    public void onBackPressed() {
        if (MediaManager.getInstance().getPlayStatus() == MediaManager.PLAY) {
            mPresenter.showConfirmQuitMeditationDialog(this);
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void update(Observable observer, Object object) {
        super.update(observer, object);
        if (observer instanceof AudioManagerEvent) {
            int focusChange = (int) object;
            if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                //获得焦点之后的操作
                MediaManager.getInstance().start();
            } else {
                //失去焦点之后的操作
                MediaManager.getInstance().pause();
            }
        }
        if (observer instanceof MediaManager) {
            MediaManager.OperatorResultBean operatorResultBean = (MediaManager.OperatorResultBean) object;
            // 获取时长
            if (operatorResultBean.operation == MediaManager.OperatorResultBean.GET_MEDIA_DURATION){
                this.duration = (int) operatorResultBean.operatorValue;
                mDataBinding.circleProgress.setMaxProgress(duration);
                mDataBinding.progressLoading.setVisibility(View.GONE);
                mDataBinding.btnPlay.setOnClickListener(this);
                MediaManager.getInstance().start();
            } else if (operatorResultBean.operation == MediaManager.OperatorResultBean.ON_PROGRESS_UPDATE){// 更新播放进度
                int progress =  (int) operatorResultBean.operatorValue;
                long leftMills = duration - progress;
                mDataBinding.tvTime.setText(DateUtil.formatTime(leftMills / 1000));
                mDataBinding.circleProgress.setProgress(progress);
            } else if (operatorResultBean.operation == MediaManager.OperatorResultBean.ON_BUFFERING_UPDATE){// 更新缓冲进度

            } else if (operatorResultBean.operation == MediaManager.OperatorResultBean.ON_MEDIA_PLAYER_START){// 开始播放
                mDataBinding.btnPlay.setText("暂停");
            } else if (operatorResultBean.operation == MediaManager.OperatorResultBean.ON_MEDIA_PLAYER_STOP){// 开始播放
                mDataBinding.btnPlay.setText("播放");
            } else if (operatorResultBean.operation == MediaManager.OperatorResultBean.ON_MEDIA_PLAYER_COMPLETION){// 播放完成
                mDataBinding.circleProgress.setProgress(0);
                mDataBinding.tvTime.setText("00:00");
                operateType = OPERATE_ADD_RECORD;
                mPresenter.AddMeditationRecord(meditationBean.CourseID, meditationBean.ID, taskId);
            } else if (operatorResultBean.operation == MediaManager.OperatorResultBean.ON_BUFFERING_START){// 开始缓冲
                mDataBinding.progressLoading.setVisibility(View.VISIBLE);
            } else if (operatorResultBean.operation == MediaManager.OperatorResultBean.ON_BUFFERING_END){// 结束缓冲
                mDataBinding.progressLoading.setVisibility(View.GONE);
            }
        }
    }

    @Override
    protected void onDestroy() {
        MediaManager.getInstance().release();
        MediaManager.getInstance().deleteObserver(this);
        AudioManagerEvent.getInstance().deleteObserver(this);
        AudioManagerEvent.getInstance().abandonAudioFocus();
        super.onDestroy();
    }
}
